package cn.itsource.user.controller;

import cn.itsource.basic.annotation.PreAuthorize;
import cn.itsource.user.service.IUserService;
import cn.itsource.user.domain.User;
import cn.itsource.user.query.UserQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping("/save")
    @PreAuthorize(name = "保存用户", sn = "user:save")
    public AjaxResult addOrUpdate(@RequestBody User user){ 
        if( user.getId()!=null){
		userService.update(user);
        }else{
                userService.insert(user);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize(name = "删除用户", sn = "user:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        userService.delete(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        userService.patchDelete(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/selectById/{id}")
    public User findOne(@PathVariable("id")Long id) {
        return userService.selectById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping("/selectAll")
    public List<User> findAll(){
        return userService.selectAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/selectByQueryPage")
    public PageResult<User> queryPage(@RequestBody UserQuery query) {
        return userService.selectByQueryPage(query);
    }

    @PutMapping("/disable/{id}")
    public AjaxResult disable(@PathVariable("id") Long id){
        return userService.disable(id);
    }

    @PutMapping("/enable/{id}")
    public AjaxResult enable(@PathVariable("id") Long id){
        return userService.enable(id);
    }

    @PostMapping("phoneRegister")
    public AjaxResult phoneRegister(@RequestBody Map<String,String> map){
        return userService.phoneRegister(map);
    }

    @PostMapping("emailRegister")
    public AjaxResult emailRegister(@RequestBody Map<String,String> map){
        return userService.emailRegister(map);
    }
    /**
     * 邮箱注册用户激活
     */
    @GetMapping("/emailRegister/active/{id}")
    public void emailRegisterActive(@PathVariable("id")Long id, HttpServletResponse response) throws IOException {
        userService.emailRegisterActive(id);
/*        response.getWriter().print("<script>\n" +
                "  window.setTimeout(function () {\n" +
                "    location.href = 'http://127.0.0.1:8081/#/login'\n" +
                "  },5000)\n" +
                "</script>");*/
    }

}
